RaisingLight.Framework.Controls.Page = RaisingLight.Framework.Controls.FrameworkElement.extend(
{
    constructor: function(canvas)    
	{
		this.base();
        this.Canvas = canvas;
        this.DrawingContext = new RaisingLight.Framework.DrawingContext(this.Canvas.getContext('2d'));
		
		// Attach the mousedown, mousemove and mouseup event listeners.
    	canvas.addEventListener('mousedown', this.OnMouseDown, false);
		canvas.addEventListener('mousemove', this.OnMouseMove, false);
    	canvas.addEventListener('mouseup',   this.OnMouseUp, false);
    },
	
    Show: function()    
    {
        this.OnRender(this.DrawingContext);
    },
	
    OnRender: function(drawingContext)
    {
        // TODO:    
        for (var i = 0; i < this.Elements.length; i++) 
        {
            this.Elements[i].OnRender(drawingContext);
        }
    },
	
	GetMouseEventPoint: function(ev)	
	{
		var x, y;
	  // Get the mouse position relative to the canvas element.
	  if (ev.layerX || ev.layerX == 0) 
	  { // Firefox
	    x = ev.layerX;
	    y = ev.layerY;
	  } 
	  else if (ev.offsetX || ev.offsetX == 0) 
	  { // Opera
	    x = ev.offsetX;
	    y = ev.offsetY;
	  }
	  return new RaisingLight.Framework.Point(x, y);
	},
	
	PointInRegion: function(point, region)	
	{
		return ((point.X > region.Location.X && point.X < (region.Location.X + region.Size.Width))&& (point.Y > region.Location.Y && point.Y < (region.Location.Y + region.Size.Height))); 
	},
	
	
	OnMouseDown: function(e)
	{
		var point = this.GetMouseEventPoint(e);
		for (var i = 0; i < this.Elements.length; i++)        
		{
			var element = this.Elements[i];
            var region = element.GetVisualRegion();
			if(this.PointInRegion(point, region))			
			{
				var elementPoint = new RaisingLight.Framework.Point(point.X - region.Location.X, point.Y - region.Location.Y);
				var mouseEventArgs = new RaisingLight.Framework.MouseEventArgs(elementPoint);
				element.OnMouseDown(mouseEventArgs);
			}
        }
    },
	
	OnMouseMove: function(e)
 	{
		var point = this.GetMouseEventPoint(e);
		for (var i = 0; i < this.Elements.length; i++)        
		{
			var element = this.Elements[i];
            var region = element.GetVisualRegion();
			if(this.PointInRegion(point, region))			
			{
				var elementPoint = new RaisingLight.Framework.Point(point.X - region.Location.X, point.Y - region.Location.Y);
				var mouseEventArgs = new RaisingLight.Framework.MouseEventArgs(elementPoint);
				element.OnMouseMove(mouseEventArgs);
			}
        }
    },
	
	OnMouseUp: function(e)
 	{
		var point = this.GetMouseEventPoint(e);
		for (var i = 0; i < this.Elements.length; i++)        
		{
			var element = this.Elements[i];
            var region = element.GetVisualRegion();
			if(this.PointInRegion(point, region))			
			{
				var elementPoint = new RaisingLight.Framework.Point(point.X - region.Location.X, point.Y - region.Location.Y);
				var mouseEventArgs = new RaisingLight.Framework.MouseEventArgs(elementPoint);
				element.OnMouseUp(mouseEventArgs);
			}
        }
    }
    
});
